package com.movitech.mobile.cz.base.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.license.LicenseUtil;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.DateUtils;
import com.movitech.mobile.cz.base.purchase.entity.WFlowFormTimeNode;
import com.movitech.mobile.cz.base.purchase.mapper.WFlowFormTimeNodeMapper;
import com.movitech.mobile.cz.base.purchase.service.WFlowFormTimeNodeService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 流程表单-时间节点计划 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-15
 */
@Service
public class WFlowFormTimeNodeServiceImpl extends ServiceImpl<WFlowFormTimeNodeMapper, WFlowFormTimeNode> implements WFlowFormTimeNodeService {

    /**
     *
     * 查询时间节点
     * @param flowFormId
     * @return
     */
    @Override
    public WFlowFormTimeNode selectByFlowFormId(String flowFormId) {
		LicenseUtil.INSTANCE.validate();
        EntityWrapper w = new EntityWrapper<>();
        WFlowFormTimeNode wFlowFormTimeNode = new WFlowFormTimeNode();
		wFlowFormTimeNode.setFlowFormId(flowFormId);
        w.setEntity(wFlowFormTimeNode);
        wFlowFormTimeNode.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        return  selectOne(w);
    }

	@Override
	public boolean updateWFlowFormTimeNode(WFlowFormTimeNode wFlowFormTimeNode) {
		LicenseUtil.INSTANCE.validate();

		if(StringUtils.isNotEmpty(wFlowFormTimeNode.getId())){
			Date updateDate = new Date();
			String userId = UserUtils.getCurrentUserId();

			wFlowFormTimeNode.setUpdateBy(userId);
			wFlowFormTimeNode.setUpdateDate(updateDate);

			int i = baseMapper.updateById(wFlowFormTimeNode);

			return i > 0;
		}

		return false;
	}

	@Override
	public boolean updateByFormId(WFlowFormTimeNode wFlowFormTimeNode) {
		LicenseUtil.INSTANCE.validate();
		wFlowFormTimeNode.setUpdateBy(UserUtils.getCurrentUserId());
		wFlowFormTimeNode.setUpdateDate(DateUtils.getNow());
		return update(wFlowFormTimeNode,new EntityWrapper<WFlowFormTimeNode>().eq("flow_form_id",wFlowFormTimeNode.getFlowFormId()));
	}
}
